package cn.lixu.system.controller;

import cn.lixu.basic.util.JsonResult;
import cn.lixu.basic.util.PageList;
import cn.lixu.system.domain.Role;
import cn.lixu.system.query.RoleQuery;
import cn.lixu.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色接口
 */
@RestController //controller层  返回的是json格式数据
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping
    public List<Role> loadAll() {
        return roleService.loadAll();
    }

    /**
     * 查询一个
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Role loadById(@PathVariable("id") Long id) {
        return roleService.loadById(id);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult deleteById(@PathVariable("id") Long id) {
        try {
            roleService.remove(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * @param role
     * @return
     */
    @PutMapping
    public JsonResult addAndUpdate(@RequestBody Role role) {
        try {
            if (role.getId() == null) {
                roleService.save(role);
            } else {
                roleService.update(role);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页数据
     *
     * @param roleQuery
     * @return
     */
    @PostMapping
    public PageList<Role> queryPage(@RequestBody RoleQuery roleQuery) {
        return roleService.queryPage(roleQuery);

    }

    /**
     * 批量删除
     *
     * @param
     * @return
     */
    @PatchMapping
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            roleService.patchDel(ids);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }


   /* @GetMapping("/roleTree")
    @ApiOperation(value = "获取菜单树-无限极接口")
    public List<Role> roleTree() {
        return roleService.roleTree();
    }*/

}

